<template>
  <van-button
    class="follow-btn"
    round
    size="small"
    v-if="value"
  >已关注
  </van-button>
  <van-button
    v-else-if="authorID === $store.state.currentUser.id"
    class="follow-btn"
    type="danger"
    disabled
    round
    size="small"
  >自己的文章
  </van-button>
  <van-button
    v-else
    class="follow-btn"
    type="info"
    color="#3296fa"
    round
    size="small"
    icon="plus"
  >关注
  </van-button>
</template>

<script>
// 导入关注用户及取消关注请求接口
import { addFollowAuthor, cancelFollowAuthor } from '@/api/users'
export default {
  name: 'FollowAuthor',
  // 接收父组件传递文章关注信息
  props: {
    // 服务器关注状态
    value: {
      type: Boolean,
      required: true
    },
    // 当前文章作者ID
    authorID: {
      type: [Number, String, Object],
      required: true
    }
  },
  data () {
    return {}
  },
  methods: {
    async switchFocushandle () {
      try {
        if (this.value) {
        // 当true为已关注用户,点击为取消关注
          await cancelFollowAuthor(this.authorID)
        } else {
        // false表示未关注状态，点击为添加关注
          await addFollowAuthor(this.authorID)
        }
        this.$emit('input', !this.value)
      } catch (err) {}
    }
  }
}
</script>

<style lang="less" scoped>

</style>
